// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
const tencentcloud = require("tencentcloud-sdk-nodejs");
const config = require("../config");

/* -------------------------------------------------------------------------- */
const FtClient = tencentcloud.ft.v20200304.Client;

const clientConfig = {
  credential: {
    secretId: config.secretId,
    secretKey: config.secretKey,
  },
  region: "ap-chengdu",
  profile: {
    httpProfile: {
      endpoint: "ft.tencentcloudapi.com",
    },
  },
};
const client = new FtClient(clientConfig);

module.exports = async function (ctx) {
  let imgBase64 = ctx.request.body.imgBase64;
  let Gender = ctx.request.body.Gender;
  console.log("Gender = ");
  console.log(Gender);
  if (!imgBase64 || !(Gender === 0 || Gender === 1)) {
    console.log("没有参数: imgBase64 或者 Gender");
    return {
      info: "请上传imgBase64和Gender",
    };
  }
  const params = {
    Image: imgBase64,
    GenderInfos: [
      {
        Gender: Gender,
      },
    ],
    RspImgType: "url",
  };
  let data = await client.SwapGenderPic(params).then(
    (data) => {
      console.log(data);
      return data;
    },
    (err) => {
      console.error("error", err);
    }
  );
  return data;
};
